
package com.shop.cloud.weixin.admin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.weixin.admin.config.ma.WxMaConfiguration;
import com.shop.cloud.weixin.common.constant.WxReturnCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序备案
 *
 * @author
 * @date 2022-03-03 15:26:16
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmaicpfiling")
@Tag(description = "wxmaicpfiling", name = "小程序备案")
public class WxMaIcpFilingController {

    /**
     * 获取小程序备案状态及驳回原因
     * @param appId
     * @return R
     */
    @Operation(summary = "获取小程序备案状态及驳回原因")
    @GetMapping("/{appId}")
    @PreAuthorize("@ato.hasAuthority('wxma:wxapp:edit')")
    public R getById(@PathVariable("appId") String appId) {
        try {
            String responseContent = WxMaConfiguration.getMaService(appId).get("https://api.weixin.qq.com/wxa/icp/get_icp_entrance_info", null);
            JSONObject jsonObject = JSONUtil.parseObj(responseContent);
            String errCode = jsonObject.getStr("errcode");
            if(WxReturnCode.SUC_0.getCode().equals(errCode)){
                return R.ok(jsonObject);
            }else {
                return R.failed(jsonObject.getStr("errmsg"));
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
            return WxReturnCode.wxErrorExceptionHandler(e);
        }
    }

}
